package com.misic.wed;

import com.alibaba.fastjson.JSONObject;
import com.misic.pojo.Admin;
import com.misic.pojo.Consumer;
import com.misic.service.CollectService;
import com.misic.service.ConsumerService;
import com.misic.utils.Consts;
import com.sun.el.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;
    @PostMapping(value = "/login")
    public Object login(Consumer consumer){
        JSONObject jsonObject = new JSONObject();
//        String username = request.getParameter("username").trim();     //账号
//        String password = request.getParameter("password").trim();     //密码
        if(consumer.getUsername()==null||consumer.getUsername().equals("")){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"用户名不能为空");
            return jsonObject;
        }
        if(consumer.getPassword()==null||consumer.getPassword().equals("")){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"密码不能为空");
            return jsonObject;
        }

//        //保存到前端用户的对象中
//        Consumer consumer = new Consumer();
//        consumer.setUsername(username);
//        consumer.setPassword(password);
        boolean flag = consumerService.verifyPassword(consumer.getUsername(),consumer.getPassword());
        if(flag){   //验证成功
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"登录成功");
            jsonObject.put("userMsg",consumerService.getByUsername(consumer.getUsername()));
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"用户名或密码错误");
        return jsonObject;
    }
    /**
     * 根据主键查询整个对象
     */
    @GetMapping(value = "/selectByPrimaryKey")
    public Object selectByPrimaryKey(@RequestParam(value = "id") Integer id){
//        String id = request.getParameter("id").trim();          //主键
        return consumerService.selectByPrimaryKey(id);
    }
    @PostMapping(value = "/update")
    public Object updateConsumer(Consumer consumer){
        System.out.println(consumer.toString());
        JSONObject jsonObject = new JSONObject();
//        String id = request.getParameter("id").trim();          //主键
//        String username = request.getParameter("username").trim();     //账号
//        String password = request.getParameter("password").trim();     //密码
//        String sex = request.getParameter("sex").trim();               //性别
//        String phoneNum = request.getParameter("phoneNum").trim();     //手机号
//        String email = request.getParameter("email").trim();           //电子邮箱
//        String birth = request.getParameter("birth").trim();           //生日
//        String introduction = request.getParameter("introduction").trim();//签名
//        String location = request.getParameter("location").trim();      //地区

        if(consumer.getUsername()==null||consumer.getUsername().equals("")){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"用户名不能为空");
            return jsonObject;
        }
        if(consumer.getPassword()==null||consumer.getPassword().equals("")){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"密码不能为空");
            return jsonObject;
        }
        //把生日转换成Date格式
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date birthDate = new Date();
//        try {
//            birthDate = dateFormat.parse(consumer.getBirth());
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        //保存到前端用户的对象中
//        Consumer consumer = new Consumer();
//        consumer.setId(Integer.parseInt(id));
//        consumer.setUsername(username);
//        consumer.setPassword(password);
//        consumer.setSex(new Byte(sex));
//        consumer.setPhoneNum(phoneNum);
//        consumer.setEmail(email);
//        consumer.setBirth(birthDate);
//        consumer.setIntroduction(introduction);
//        consumer.setLocation(location);
        boolean flag = consumerService.update(consumer);
        if(flag){   //保存成功
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"修改失败");
        return jsonObject;
    }

}
